<?php

namespace App\Entities;

use Illuminate\Database\Eloquent\Model;
use Nestable\NestableTrait;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

class Category extends Model implements Transformable
{
    use TransformableTrait, NestableTrait;

    public $table = 'categories';

    protected $fillable=[
        'parent_id','name','module_id','seo_title','keys','type','sort','intro'
    ];

    protected $dates = ['deleted_at'];

    protected static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        //删除关联数据
        self::deleted(function($model){
            $model->article()->detach();
            $model->picture()->detach();
            $model->vidio()->detach();
            $model->download()->detach();
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function article(){
        return $this->belongsToMany('App\Entities\Article', 'articles');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function download(){
        return $this->belongsToMany('App\Entities\Download', 'downloads');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function picture(){
        return $this->belongsToMany('App\Entities\Picture', 'pictures');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function vidio(){
        return $this->belongsToMany('App\Entities\Vidio', 'vidios');
    }

    /**
     * @return mixed
     */
    public function categoryModule(){
        return $this->hasOne('App\Entities\Module','id','module_id');
    }

    /**
     * @return mixed
     */
    public function categoryArticle(){
        return $this->hasOne('App\Entities\Article','id','category_id');
    }



}
